package jstart.exe;

import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.swing.*;

/**
 * @author zwz
 * Created on 2021-02-08
 */
class Kits {

    static void loadProps(Properties properties, File propFile) {
        try (InputStream ins = new FileInputStream(propFile)) {
            properties.load(ins);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void loadProps(Properties properties, InputStream ins) {
        try {
            properties.load(ins);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static boolean isNotVisibleString(String str) {
        return str == null || str.isEmpty() || str.trim()
                                                  .isEmpty();
    }

    static boolean isVisibleString(String str) {
        return !isNotVisibleString(str);
    }

    static void showMessageBox(Component parent, boolean error, String message) {
        if (error) {
            JOptionPane.showMessageDialog(parent, message, "错误", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(parent, message, "提示", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    static void emptyDir(File dir) {
        if (dir == null || !dir.exists()) return;
        File[] files = dir.listFiles();
        if (files == null || files.length == 0) return;

        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            } else if (f.isDirectory()) {
                removeDir(f);
            }
        }
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    private static void removeDir(File dir) {
        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            dir.delete();
            return;
        }

        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            } else if (f.isDirectory()) {
                removeDir(f);
            }
        }
        dir.delete();
    }
}
